package org.ucas.faker.web.fetcher.cond;

import org.ucas.faker.web.fetcher.DynamicSqlException;
import org.ucas.faker.web.fetcher.meta.ColumnField;
import org.ucas.faker.web.fetcher.meta.Table;

import java.io.Serializable;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class CompCond extends FetchCond implements Serializable {

  private final static Set<String> acceptable = new HashSet<String>(){{
    add("<");
    add("<=");
    add("=");
    add(">=");
    add(">");
    add("IS");
    add("<>");
  }};

  public String comp;

  public ColumnField target;

  public ColumnField column;

  public CompCond() {

  }

  public CompCond(ColumnField column, String comp, ColumnField target) {
    this.column = column;
    this.comp = comp;
    this.target = target;
  }

  @Override
  public String sql() {
    if(!acceptable.contains(comp)) throw new DynamicSqlException("Not Supported comp op : " + comp);

    if(target == null) throw new DynamicSqlException("Lack the comp target for " + column.sql());

    return column.sql() + " " + comp + " " + target.sql();
  }

  @Override
  public void replace(Table src, Table dst) {
    if(column != null) column.replace(src,dst);
    if(target != null) target.replace(src,dst);
  }
}
